package com.lanqiao.guimei.controller;

import com.lanqiao.guimei.dao.ProductDao;
import com.lanqiao.guimei.dao.SaleProdMainDao;
import com.lanqiao.guimei.vo.Page;
import com.lanqiao.guimei.vo.Product;
import com.lanqiao.guimei.vo.SaleProd;
import com.lanqiao.guimei.vo.Stock;
import com.lanqiao.web.mvc.annotation.Controller;
import com.lanqiao.web.mvc.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:沈林玉
 * @date:2019/8/16 9:00
 * @Description:
 */
@Controller
public class SaleProdMainController {
    SaleProdMainDao saleProdMainDao = new SaleProdMainDao();

    /**
     * 1.在线商品查询
     * @param saleProd
     */
    @RequestMapping("/saleProd/select")
    public String selectAllSaleProd(HttpServletRequest request, HttpSession session, SaleProd saleProd,Stock stock){
        Page<SaleProd> saleProdPage = (Page<SaleProd>)session.getAttribute("saleProdPage");
        String pageNo = request.getParameter("pageNo");

        if (saleProdPage==null || StringUtils.isBlank(pageNo)) {
            saleProdPage = new Page<SaleProd>();
            saleProdPage.setPageSize(5);
            saleProdPage.setPageNo(1);
            saleProdPage.setQueryObject(saleProd);
            // 获取总记录数
            int countSaleProd = saleProdMainDao.getCountSaleProd(saleProdPage);
            saleProdPage.setTotalRow(countSaleProd);
        }else{
            saleProdPage.setPageNo(Integer.valueOf(pageNo));
        }
        session.setAttribute("saleProdPage",saleProdPage);

        List<SaleProd> saleProdList = new SaleProdMainDao().selectSaleProdByCondition(saleProdPage);
        request.setAttribute("saleProdList",saleProdList);
        return "/after/product_maintain.jsp";
    }

    /**
     * 1.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byProdNo")
    public String selectSaleProdByProdNo(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByProdNo(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByProdNoList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }
    /**
     * 2.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/bySalerName")
    public String selectSaleProdBySalerName(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdBySalerName(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdBySalerNameList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }

    /**
     * 3.按一口价查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byRealPrice")
    public String selectSaleProdByRealPrice(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByRealPrice(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByRealPriceList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }
    /**
     * 4.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byStartTime")
    public String selectSaleProdByStartTime(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByStartTime(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByStartTimeList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }

    /**
     * 5.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byEndTime")
    public String selectSaleProdByEndTime(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByEndTime(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByEndTimeList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }

    /**
     * 6.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byStartQuantity")
    public String selectSaleProdByStartQuantity(HttpServletRequest request, Stock stock,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByStartQuantity(saleProd,stock);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByStartQuantityList",saleProdList);
            return "/after/product_maintain.jsp";
        }
        return null;
    }

    /**
     * 7.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byEndQuantity.do")
    public String selectSaleProdByEndQuantity(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByEndQuantity(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByEndQuantityList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }

    /**
     * 8.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byCarriage")
    public String selectSaleProdByCarriage(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByCarriage(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByCarriageList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }

    /**
     * 9.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byProdSkin")
    public String selectSaleProdByProdSkin(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByProdSkin(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByProdSkinList",saleProdList);
            return "/after/selectSaleProd.jsp";
        }
        return null;
    }

    /**
     * 10.按卖家查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    @RequestMapping("/selectSaleProd/byLocation")
    public String selectSaleProdByLocation(HttpServletRequest request,SaleProd saleProd){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByLocation(saleProd);
        if (saleProdList!=null){
            request.setAttribute("SaleProdByLocationList",saleProdList);
            return "/after/product_maintain.jsp";
        }
        return null;
    }

    /**
     * 11.按条件查询在线商品
     * @param request
     * @param saleProd
     * @return
     */
    /*@RequestMapping("/selectSaleProd/byCondition")
    public String selectSaleProdByCondition(HttpServletRequest request,SaleProd saleProd,Stock stock){
        List<SaleProd> saleProdList = saleProdMainDao.selectSaleProdByCondition(saleProd,stock);
        if (saleProdList!=null){
            request.setAttribute("saleProdList",saleProdList);
            return "/after/product_maintain.jsp";
        }
        return null;
    }*/

    /**
     * 12.下架在线商品
     * @param saleProd
     * @return
     */
    @RequestMapping("/saleProd/doDelete")
    public Map deleteSaleProd(SaleProd saleProd){
        boolean deleted = saleProdMainDao.deleteSaleProd(saleProd);
        Map json = new HashMap();
        if (deleted){
            json.put("result",true);
            return json;
        }else{
            json.put("result",false);
        }
        return json;
    }

}
